package com.metacube.questionbank.dao;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import com.metacube.questionbank.jdbc.QuestionAnswerRowMapper;
import com.metacube.questionbank.model.Answer;
import com.metacube.questionbank.model.Question;

/*
 * @author Team Neeraj Jain
 * */
public class AnswerDAO {
	@Autowired
	JdbcTemplate jdbcTemplate;
	@Autowired
	DataSource dataSource;

	@PostConstruct
	public void setJdbcTemplate() {
		jdbcTemplate.setDataSource(dataSource);
	}

	/*
	 * @param answer object of answer class This method adds an answer in the
	 * database.
	 */
	public void insertAnswer(Answer answer) {
		String sql = "INSERT INTO answers (answer, user_id,ques_id) VALUES (?,?,?)";
		jdbcTemplate.update(sql,
				new Object[] { answer.getAnswerDesc(), answer.getUserId(),
						answer.getQuestionId() });
	}

	/*
	 * @param qId id of question This method returns the answers of the question
	 * whose id is passed in the argument.
	 * 
	 * @return list of answers or null;
	 */
	public List<Answer> getAnswers(int quesId) {
		List<Answer> qUList = new ArrayList<Answer>();
		String sql = "select * from answers where ques_id=" + quesId;
		qUList = jdbcTemplate.query(sql, new QuestionAnswerRowMapper());
		return qUList;
	}

	/*
	 * @param userId id of user This method returns the answers which are given
	 * by a particular user.
	 * 
	 * @return list of answers or null
	 */
	public List<Answer> getAnswersByUserId(int userId) {
		List<Answer> qUList = new ArrayList<Answer>();
		String sql = "select answers.answer_Id,answer,user_Id,ques_id,answer_date from answers natural join users where user_id="
				+ userId;
		qUList = jdbcTemplate.query(sql, new QuestionAnswerRowMapper());
		return qUList;
	}

	/*
	 * @param userId id of user This method returns the answers which are liked
	 * by a particular user.
	 * 
	 * @return list of answers or null
	 */
	public List<Answer> getAnswerLikedByUser(int userId) {
		List<Answer> qUList = new ArrayList<Answer>();
		String sql = "select answers.answer_Id,answer,answers.user_Id,ques_id,answer_date from answers join votes where answers.answer_id=votes.answer_id  and upvote_status=1 and votes.user_id="
				+ userId;
		qUList = jdbcTemplate.query(sql, new QuestionAnswerRowMapper());
		System.out.println("answerLike" + qUList);
		return qUList;
	}

	/*
	 * @param question question This method returns answer count.That is,it
	 * finds answer count for question
	 * 
	 * @return answer count
	 */
	public Integer getAnswerCount(Question question) {
		int count;
		String sql = "select Count(ques_id) from answers where ques_id="
				+ question.getQuesId();
		count = jdbcTemplate.queryForInt(sql);
		return count;
	}

	/*
	 * @param answerId id of answer This method returns the id of user who
	 * posted the answer
	 * 
	 * @return userId id of user who posted the answer
	 */
	public int getUserIdByAId(int answerId) {
		int userId = 0;
		String sql = "select user_Id from answers where answer_id=" + answerId;
		userId = jdbcTemplate.queryForInt(sql);
		return userId;
	}
}
